"use strict";
const FM = require("../handler/FoodManager");
const FoodEffect = FM.FoodEffect;
const Food = FM.Food;
const FoodSet = FM.FoodSet;
const FoodSetManager = FM.FoodSetManager;

const food_set_manager = FM.food_set_manager;

const range_width = FM.range_width;
const range_height = FM.range_height;

const unit_range_width = FM.unit_range_width;
const unit_range_height = FM.unit_range_height;

const x_len = range_width / unit_range_width;
const x_len_2 = x_len / 2;
const y_len = range_height / unit_range_height;
const y_len_2 = y_len / 2;
//初始化食物
for (let x = 0; x < x_len; x += 1) {
	for (let y = 0; y < y_len; y += 1) {
		let _x = x * unit_range_width + (Math.random() - 0.5).toFixed(2) * 2 * unit_range_width
		let _y = y * unit_range_height + (Math.random() - 0.5).toFixed(2) * 2 * unit_range_height
		let _add_body /*[1~4)*/ = ((x_len_2 - Math.abs(x_len_2 - x)) / x_len_2 + (y_len_2 - Math.abs(y_len_2 - y)) / y_len_2) /*[0~2)*/ * Math.random().toFixed(2) + 1 /*(1~2)*/
		let food = new Food(Math.round(_x), Math.round(_y), {
			add_body: _add_body
		});
		food_set_manager.addFood(food)
	}
}

module.exports = function(app) {
	return new FoodRemote(app);
};

function FoodRemote(app) {
	this.app = app;
	this.channelService = app.get('channelService');
};
const FoodRemoteProto = FoodRemote.prototype;

FoodRemoteProto.getRangeFoods = function(x, y, width, height, cb) {
	var res = food_set_manager.getRangeFoods(x, y, width, height);
	cb && cb(res);
}

FoodRemoteProto.removeFoods = function(foods, cb) {
	var res = [];

	foods.forEach(food_info => {
		var food = food_set_manager.removeFood(food_info.x, food_info.y);
		if (food) {
			res.push(food)
		}
	});

	cb && cb(res);
}

FoodRemoteProto.addFoods = function(foods, cb) {
	foods.forEach(function(food_info) {
		var food = new Food(food_info.x, food_info.y, food_info.effect);
		food_set_manager.addFood(food)
	});
	cb && cb()
}